package com.kaili.controller.web.v1;

import com.alibaba.fastjson.JSON;
import com.kaili.common.tsp.TSP;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/operation/tsp")
@Api(tags = "最佳路径匹配", description = "最佳路径匹配相关API")
public class TspController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @ApiOperation(value = "获取最佳路线")
    @GetMapping("getBestTour")
    public String getBestTour(@RequestParam("distance") String[] distance) {
        logger.info("Enter in getBestTour.TSP算法-距离矩阵："+ JSON.toJSONString(distance));

        double[][] distanceArray = toArray(distance);
        return TSP.calc(distanceArray);
    }

    /**
     * 一维数组转二维数组
     * @param distance
     * @return
     */
    private double[][] toArray(String[] distance){
        double[][] distanceArray = new double[distance.length][distance.length];
        for(int i=0;i<distance.length;i++){
            String str = distance[i];
            String[] strArray = str.split("_");
            for(int j=0;j<strArray.length;j++){
                distanceArray[i][j]=Double.parseDouble(strArray[j]);
            }
        }

        return distanceArray;
    }
}
